/**
 * @(#)ChinaCodeService.java, 2013-2-14. Copyright 2013 RENREN, Inc. All rights
 *                            reserved. RENREN PROPRIETARY/CONFIDENTIAL. Use is
 *                            subject to license terms.
 */
package com.wennimei.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

import com.wennimei.domain.ChinaCode;
import com.wennimei.util.DataUtil;
import com.wennimei.util.FileProcessor;
import com.wennimei.util.FileUtil;

/**
 * @author shichen.feng@renren-inc.com
 */
@Service
public class ChinaCodeService {

    private static final Map<String, ChinaCode> map = new HashMap<String, ChinaCode>();

    @PostConstruct
    void init() {
        FileUtil.processFile(DataUtil.CHINA_CODE, new FileProcessor() {

            @Override
            public void process(String line) {
                String[] array = line.split(" ");
                ChinaCode code = null;
                code = new ChinaCode(array[0], array[1], array[2], array[3]);
                map.put(code.getDistrict(), code);
            }
        });
    }

    public String get(String content) {
        ChinaCode code = null;
        for (String s: map.keySet()) {
            if (content.contains(s)) {
                code = map.get(s);
                break;
            }
        }
        if (null == code)
            return "暂未查到该市区的邮编/区号信息。";

        StringBuilder sb = new StringBuilder();
        sb.append(code.getCity()).append(",").append(code.getDistrict())
                .append(",邮编").append(code.getZipCode()).append(",区号")
                .append(code.getAreaCode());
        return sb.toString();
    }
}
